*** BCG: results for the estimation sample
* Bustein, Carvalho and Grassi
***


****************************** 1. Preamble ******

* clean up 
	clear all 
	macro drop _all
	set max_memory 50g
	set matsize 11000

*set working directory. Must contain:
	cd "C:\Users\Public\Documents\BCG_DGM\BCG\replication_file_jan25_package\"
	

****************************************CHANGE SPECS AND OUTLIER ***************	
local specsDeta_l    _estimsample
local winlevel_l    03   
********************************************************************************


	di "===============Quick control================="
	di "specsDeta_l=`specsDeta_l'"
	di "winlevel_l=`winlevel_l'"	
	
	**********************CHOICE of outliuer treatment level
	* winsorization level 
	global winlevel_gl `winlevel_l'
	di "winlevel_gl=$winlevel_gl"

	*trimmeing level (thousands)
	if "`winlevel_l'"=="01"{
		global toptr_gl 990
		global bottr_gl 10
	}
	else if "`winlevel_l'"=="015"{
		global toptr_gl 985
		global bottr_gl 15
	}
	else if "`winlevel_l'"=="02"{
		global toptr_gl 980
		global bottr_gl 20
	}
	else if "`winlevel_l'"=="025"{
		global toptr_gl 975
		global bottr_gl 25
	}
	else if "`winlevel_l'"=="03"{
		global toptr_gl 970
		global bottr_gl 30
	}
	
	di "toptr_gl = $toptr_gl"
	di "toptr_gl = $bottr_gl"

		
	*markup complementaire spec
	global specsDeta `specsDeta_l'
	di "specsDeta (global)==$specsDeta"	
	
	di "results ending in  _$winlevel_gl$specsDeta" 
	
	
	
	*choose the dataset

	* select dataset with production data 
	global dataset "ficusfare_reduced_9419_sec2_v0222"
	local indlist 08 13 14 15 16 17 18 20 22 23 24 25 26 27 28 29 30 31 32 33 43 46 70 95
	
	di "data=$dataset"
	
	di "==================================="
		

******************************************************************************************
****************************** 2. Load the data ******
******************************************************************************************
	
*select path
cd "C:\Users\Public\Documents\BCG_DGM\BCG\replication_file_jan25_package\"

*load the firm-level data:
	
	
	use data/$dataset, clear 
	
	**drop some useless variables
	capture drop acha1 acha2 acha3 acha5 acha6 cogs cogs_newdef acha1R acha2R acha3R acha5R acha6R  invcorp catotal_tot

	**destring the sector code before the merge
	destring naf2d, replace

	
*Compute Markup 

	** define production variables
	gen s = ln(catotalR)
	gen m = ln(acha4R)	//m will be the variable input 
	gen v = ln(salR)
	gen k = ln(immocorR)
	gen o = ln(autachaR)

	** define cost ratio 
	gen ratio = catotal/acha4 
	
	**Loop over the quantity and revenu markup
	foreach specs in FSQ   {
		***merge coefficients 
		merge m:1 naf2d using "data\coefficients_byind_Python_`specs'_baseline.dta", nogen
		
		***rename coefficients 
		foreach beta in const_cd m_cd v_cd o_cd k_cd const_tl m_tl v_tl o_tl k_tl m2_tl mv_tl mo_tl mk_tl v2_tl vo_tl vk_tl o2_tl ok_tl k2_tl {
			rename beta_`beta' beta_`beta'_`specs'
		}
		
		***drop redundant variables
		drop N med_CD med_TL iqr_CD iqr_TL  rho_cd  rho_tl 
		
		
		***calculate elasticity - TL 
		gen elast_tl_s`specs'_t1 = beta_m_tl_`specs'	 				 	 + 	/// 
													2*beta_m2_tl_`specs'*m   + 	///
													  beta_mv_tl_`specs'*v 	 +	///
													  beta_mo_tl_`specs'*o 	 +	///
													  beta_mk_tl_`specs'*k	 
												
		***define elasticity - CD 									
		gen elast_cd_s`specs'_t1 = beta_m_cd_`specs'
		
		
		***calculate markups 
		gen mu_sepcal_TL_s`specs'_t1 =  elast_tl_s`specs'_t1 * ratio 
		gen mu_sepcal_CD_s`specs'_t1 =  elast_cd_s`specs'_t1 * ratio
			
		***calculate log markups
		gen l_mu_sepcal_TL_s`specs'_t1 = log(mu_sepcal_TL_s`specs'_t1) 
		gen l_mu_sepcal_CD_s`specs'_t1 = log(mu_sepcal_CD_s`specs'_t1)
	}
	
	


**drop some useless variables
capture drop ml_* ratio_* elast_*	
	


************************************************************************************************************************************************************************************
******************************************************************* 3. Selecting Final Sample and Some indicators
************************************************************************************************************************************************************************************	
	
*Keep firms with positive VA	
	keep if va>0

*Keep firms with positive catotal and inputs
	/*
	gen sample_firm_inputs = 0
	replace sample_firm_inputs = 1 if (catotalR>0 & salR>0 & immocorR>0 & acha4R>0 & autachaR>0)
	label var sample_firm_inputs "=1 if (catotalR>0 & salR>0 & immocorR>0 & acha4R>0 & autachaR>0)"
	*/
	keep if sample_firm_inputs==1

*Keep the relevent sectors
	keep if sample_sectors2==1 //should be no change
	keep if naf2d_num != 95 & naf2d_num != 70 //Two other sectors that we want to drop


*Some indicator of markup level (quantity)
	gen markup_pos = 0
	replace markup_pos = 1 if mu_sepcal_TL_sFSQ_t1 > 0

	gen markup_half = 0
	replace markup_half = 1 if mu_sepcal_TL_sFSQ_t1 > 0.5
	
	gen markup_one = 0
	replace markup_one = 1 if mu_sepcal_TL_sFSQ_t1 > 1
	

*Keep siren with more than one year
	bysort siren: egen count_siren=count(catotal)
	keep  if count_siren>1
	
	
	
*Defined the estimation sample (only the EAP years, firms with price info and with emp>2)
	capture drop sample_estim 
	gen sample_estim =  sample_years * sample_firm_price * sample_firm_emp2
	
	keep if sample_sectors2==1 & sample_years ==1 & sample_firm ==1 & sample_va ==1
	
	**Compute firms that appears only once in the estimation sample
	gen catotal_estim  = catotal
	replace catotal_estim = . if sample_estim != 1
	
	**Count the number of non-missing catotal_estim
	bysort siren: egen count_siren_sample_estim=count(catotal_estim) // (for this siren 0 = not in estim, 1=appears once in estim, 2+ = nb of years in estim)
	
	** Indicator
	gen more_1year_estim  = count_siren_sample_estim != 1
	
	**Drop the firms appearing once from the sample_estim
	replace sample_estim = sample_estim * more_1year_estim //sample estim has 220733 obs as in ficusfare_reduced_9419_sec2_year2_firm_va_sevyears_v0222 after droping sector 70 and 95
	
******** MAIN SAMPLE DEFINITION FOR THIS FILE!!! SAMPLE ESTIM FIRMS ONLY ******************
	keep if sample_estim==1
*******************************************************************************************	

*Construct Sector-Level VA

	** Sector-level sum of va
	bysort naf_single year: egen va_total = total(va) 
	gen l_va_total = ln(va_total)
	
	
	**Get the list of 5-digits sector to drop
	local Nperiods 11
	preserve
		
		duplicates drop naf_single year , force 	
		keep naf_single year va_total  l_va_total 
		
		bysort naf_single: egen count_naf=count(l_va_total)
		
		duplicates drop naf_single,  force 	
		ta naf_single if count_naf<`Nperiods'
		
		keep if count_naf<`Nperiods'
		
		save  BCG/data/incomplete_naf_single_l_va_estimsample_vaggM.dta, replace
		
	restore
	
	
	
*Indicator of sector with complete VA series
	**indicator of bad sectors (manually)
	capture drop sample_sector_5d
	gen sample_sector_5d = 1
	foreach naf_sec in 13.96Z 16.22Z 24.46Z 27.31Z 27.33Z 28.24Z 28.99A 30.12Z 31.09A 33.11Z 33.13Z 33.17Z 33.19Z 33.20B 33.20D 43.11Z 43.21B 46.12A 46.47Z 46.48Z 46.65Z 46.73B{
		replace sample_sector_5d = 0 if naf_single=="`naf_sec'" 
	}
	
	**indicator of bad sectors (systematically)
	merge m:1 naf_single using "BCG/data/incomplete_naf_single_l_va_estimsample_vaggM.dta"
	gen sample_sector_5d_v2 = ( _merge!=3 )
	drop _merge count_naf
		
	** Drop obs for these sectors
	ta sample_sector_5d sample_sector_5d_v2
	keep if sample_sector_5d_v2==1
	
*Get the list of 5-digits sector to drop for positive markup firms
	local Nperiods 11
	preserve
		
		** Sector-level sum of va for positive markup firms
		gen va_pos = .
		replace va_pos = va if markup_pos==1
		
		bysort naf_single year: egen va_pos_total = total(va_pos) 
		gen l_va_pos_total = ln(va_pos_total)
		
		su va_pos va l_va_pos_total l_va_total
		
		** make it a sector level database
		duplicates drop naf_single year , force 	
		keep naf_single year va_pos_total  l_va_pos_total 
		
		bysort naf_single: egen count_naf=count(l_va_pos_total)
		
		duplicates drop naf_single,  force 	
		ta naf_single if count_naf<`Nperiods'
		
		keep if count_naf<`Nperiods'
		
		save  BCG/data/incomplete_naf_single_l_va_pos_estimsample_vaggM.dta, replace
		
	restore //one sector to drop beyond the one dropped previously
	
*Indicator of sector with complete VA series for positive markup sample
	**indicator of bad sectors (manually)
	capture drop sample_sector_5d_pos
	gen sample_sector_5d_pos = 1
	foreach naf_sec in 30.30Z{
		replace sample_sector_5d_pos = 0 if naf_single=="`naf_sec'" 
	}
	
	**indicator of bad sectors (systematically)
	merge m:1 naf_single using "BCG/data/incomplete_naf_single_l_va_pos_estimsample_vaggM.dta"
	gen sample_sector_5d_pos_v2 = ( _merge!=3 )
	drop _merge count_naf
		
	** Drop obs for these sectors
	ta sample_sector_5d_pos sample_sector_5d_pos_v2
	keep if sample_sector_5d_pos_v2==1
	
*Aggregate-level sum of va
	**For the full sample
	bysort year: egen va_agg = total(va)
	gen l_va_agg = ln(va_agg)

	
		

************************************************************************************************************************************************************************************
******************************************************************* 4. Construct Regression Variables
************************************************************************************************************************************************************************************	
		
		
* Sector-Level VA (at naf_single level)
local Nperiods 11
preserve
	
	**Make the data a sector-level panel
	duplicates drop naf_single year , force 	
	keep naf_single year va_total l_va_total 

	**set up panel
	egen naf_single_num = group(naf_single)
	xtset naf_single_num year
	
	**fix gaps (hp filter does not run with gaps but now it should no be any of these gaps)
	drop if l_va_total == .  //0 changes
	bysort naf_single: drop if _N < `Nperiods' //0 changes

	** generate HP-filtered data 
	tsfilter hp x = l_va_total, smooth(6.25) trend(l_va_total_hp)
	drop x
	gen l_va_total_hp_dev = l_va_total - l_va_total_hp
	
	** generate Hamilton-filtered data 
	hamiltonfilter l_va_total, frequency(yearly) stub(l_va_total_ha) 
	gen l_va_total_ha_dev = l_va_total - l_va_total_ha_trend
	
	** generate First-Difference-filtered data 
	xtset naf_single_num year
	gen  l_va_total_FD = l_va_total - L.l_va_total
	
	** generate Demean data 
	by naf_single_num: egen l_va_total_mean = mean(l_va_total)
	gen  l_va_total_demean = l_va_total - l_va_total_mean	
	* save results 
	save BCG/data/l_va_total_hp_${specsDeta}_estimsample_vaggM, replace

restore

* Aggregate-Level VA 
preserve
	**Make the data a sector-level panel
	duplicates drop year , force 	
	keep year va_agg l_va_agg  
	**Set up time serie
	tset year 
	
	**demean aggregate GDP
	egen l_va_agg_mean = mean(l_va_agg)
	gen l_va_agg_demean = l_va_agg - l_va_agg_mean
	
	**FD of GDP
	gen FD_l_va_agg = l_va_agg - L.l_va_agg

	**generate HP-filtered data for full sample
	tsfilter hp x = l_va_agg, smooth(6.25) trend(l_va_agg_hp)
	drop x
	gen l_va_agg_hp_dev = l_va_agg - l_va_agg_hp
	
	
	** generate Hamilton-filtered data 
	hamiltonfilter l_va_agg, frequency(yearly) stub(l_va_agg_ha) 
	gen l_va_agg_ha_dev = l_va_agg - l_va_agg_ha_trend
	
	
	**save results 
	save BCG/data/l_va_agg_hp_${specsDeta}_estimsample_vaggM, replace

restore

*Merging with sector-level HP-filtered data for all firms
	merge m:1 naf_single year using "BCG/data/l_va_total_hp_${specsDeta}_estimsample_vaggM.dta" 
	drop _merge 

*Merging with aggregate level HP-filtered data
	merge m:1 year using "BCG/data/l_va_agg_hp_${specsDeta}_estimsample_vaggM.dta" 
	drop _merge 

*Market Share
	
	**Sector level revenue
	bysort naf_single year: egen catotal_total = total(catotal)
	
	**Market Share
	gen share5 = catotal/catotal_total
	
	**Some summary stats
	su share5  


*Interaction market share with sector output	
	**HP-Deviation
		***Interaction market share * sector output: (market share * HP-filtered (log) sector-level VA)
			gen l_va_total_hp_d_inter = l_va_total_hp_dev*share5	 
			
		
	** Hamilton-filter
		***Interaction market share * sector output: (market share * HA-filtered (log) sector-level VA)
			gen l_va_total_ha_d_inter = l_va_total_ha_dev*share5	 
	
	**First-Difference (log)
		***Interaction market share * sector output: (market share * FD(log) sector-level VA)
			gen l_va_total_FD_d_inter = l_va_total_FD*share5	 
			
	
	**Demeaned
		***Interaction market share * sector output: (market share * Demean (log) sector-level VA)
			gen l_va_total_demean_d_inter = l_va_total_demean*share5	 
			
	

*Revenue Share in total revenue
	
	**Yearly total revenue
	bysort  year: egen catotal_aggr = total(catotal)
	
	**Revenue Share
	gen revShare = catotal/catotal_aggr
	
	**Some summary stats
	su revShare  

	


*Inverse markup
	xtset firmsId year

	**level
	gen inv_mu_sepcal_TL_sFSQ_t1 = 1/mu_sepcal_TL_sFSQ_t1
			
	**in first-diff			
	gen FD_inv_mu_sepcal_TL_sFSQ_t1  = inv_mu_sepcal_TL_sFSQ_t1 - L.inv_mu_sepcal_TL_sFSQ_t1
	


*Compute number of firms in each 5digit sectors
	bysort naf_single year: egen firmcount_naf_va=count(va)
	bysort naf_single year: egen firmcount_naf_ca=count(catotal)
	bysort naf_single year: egen firmcount_naf_mu=count(mu_sepcal_TL_sFSQ_t1)
	su firmcount_naf_*
	
	*since all the same let us keep only one
	drop firmcount_naf_ca firmcount_naf_mu
	*rename  firmcount_naf_ca firmcount_naf
	

	
	
	


*****************************************************************************************************************************************************************
******************************************************************* 5. Firm-Level Regressions
*****************************************************************************************************************************************************************


		
		
*************** 5.1 inverse markup on market share 


	**Winsorized the LHS
		local top_tr $toptr_gl
		local bot_tr $bottr_gl
		local winlevel $winlevel_gl
		
		gen mu_sepcal_TL_sFSQ_t1_pos = .
		replace mu_sepcal_TL_sFSQ_t1_pos = mu_sepcal_TL_sFSQ_t1 if markup_pos==1
		
		gen inv_mu_sepcal_TL_sFSQ_t1_pos = .
		replace inv_mu_sepcal_TL_sFSQ_t1_pos = inv_mu_sepcal_TL_sFSQ_t1 if markup_pos==1
		
		
		winsor  inv_mu_sepcal_TL_sFSQ_t1_pos , p(.`winlevel') gen(inv_mu_sepcal_TL_sFSQ_t1_pos_w)
		
	**Some summarry stats
		su inv_mu_sepcal_TL_sFSQ_t1_pos_w  share5 if markup_pos==1
	
	
			****In first-difference
			
					*****Compute the LHS and RHS (and winsorized)
						******set up panel
						xtset firmsId year
						
						******Compute First-Difference
						gen FDinv_mu_sepcal_TL_sFSQ_t1_pos = inv_mu_sepcal_TL_sFSQ_t1_pos - L.inv_mu_sepcal_TL_sFSQ_t1_pos
						gen FDshare5 = share5 - L.share5
						
						local top_tr $toptr_gl
						local bot_tr $bottr_gl
						local winlevel $winlevel_gl
										
						winsor  FDinv_mu_sepcal_TL_sFSQ_t1_pos , p(.`winlevel') gen(FDinv_mu_sepcal_TL_sFSQ_t1_pos_w)

		
						**Some summarry stats
						su FDinv_mu_sepcal_TL_sFSQ_t1_pos_w FDshare5 if markup_pos==1
										
						
					
					*****Regressions
						*with no FE
						reghdfe FDinv_mu_sepcal_TL_sFSQ_t1_pos_w  FDshare5  if markup_pos==1 & year>1994, noabsorb cl(year firmsId) 
						outreg2 using BCG\results_sl\MarkupMShare_FD_$winlevel_gl$specsDeta , replace tex   keep( FDshare5 ) addtext(Firm FE, No, Year FE, No, Cluster , Firm Year)
										
						
							
					
	



*************** 5.2 Firm markup on sector output   
	**Sorting the data
		xtset firmsId year

	
		
	**Taking First-Difference
		gen FD_l_mu_sepcal_TL_sFSQ_t1 = l_mu_sepcal_TL_sFSQ_t1 - L.l_mu_sepcal_TL_sFSQ_t1

	**Winsorized the LHS
		local top_tr $toptr_gl
		local bot_tr $bottr_gl
		local winlevel $winlevel_gl
				
		winsor  l_mu_sepcal_TL_sFSQ_t1 , p(.`winlevel') gen(l_mu_sepcal_TL_sFSQ_t1_w)
		winsor  FD_l_mu_sepcal_TL_sFSQ_t1 , p(.`winlevel') gen(FD_l_mu_sepcal_TL_sFSQ_t1_w)
		
	
	
	
				
		**Regressions for specification in FD
			
			
			
			reghdfe FD_l_mu_sepcal_TL_sFSQ_t1_w  l_va_total_FD l_va_total_FD_d_inter  if markup_pos==1 & year>1994 , noabsorb  cl(naf_single_num#year)
			outreg2 using BCG\results_sl\MarkupSectorOutput_$winlevel_gl$specsDeta , replace tex   keep( l_va_total_FD l_va_total_FD_d_inter) addtext(Firm FE, No, Year FE, No, Cluster , Sector * Year)
			*

	
			
	
					
					
*************** 5.3 Market Share Cyclicality

	***** 5.3.1 Market Share

		*Computed average market share
			**Compute average firm-level market share over full sample
			bysort firmsId: egen share5_m = mean(share5)
			su share5_m, d 

		*** compute log
			gen l_share5 = log(share5)
		

	
		*** FD 
		xtset firmsId year
		capture gen FDl_share5 = l_share5 -L.l_share5
		
		local top_tr $toptr_gl
		local bot_tr $bottr_gl
		local winlevel $winlevel_gl
				
		winsor  FDl_share5 , p(.`winlevel') gen(FDl_share5_w)
		
		
			***All firms
			reghdfe FDl_share5_w l_va_total_FD  if markup_pos==1 & year>1994, noabsorb  cl(naf_single_num#year)
			outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , replace tex   keep(l_va_total_FD) addtext(Firm FE, No, Year FE, No, Cluster , Sector x Year,Firms, All Filter, FD)
		
			distinct firmsId if markup_pos==1  & year>1994 & FDl_share5_w~=. &   l_va_total_FD ~= .
		
			***Large firms
			reghdfe FDl_share5_w l_va_total_FD  if markup_pos==1 & share5_m>0.5 & year>1994, noabsorb  cl(naf_single_num#year) 
			outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_FD) addtext(Firm FE, No, Year FE, No, Cluster , Sector x Year, Firms, Large>0.5,  Filter, FD)
			
			distinct firmsId if markup_pos==1 & share5_m>0.5 & year>1994 & FDl_share5_w~=. &   l_va_total_FD ~= .
			
			***Small firms
			reghdfe FDl_share5_w l_va_total_FD if markup_pos==1 & share5_m<=0.5 & year>1994, noabsorb  cl(naf_single_num#year)
			outreg2 using BCG\results_sl\marketShare_SectorOutput_$winlevel_gl$specsDeta , append tex   keep(l_va_total_FD) addtext(Firm FE, No, Year FE, No, Cluster , Sector x Year,  Firms, Small<0.5,  Filter, FD)
			
			distinct firmsId if markup_pos==1 & share5_m<=0.5 & year>1994 & FDl_share5_w~=. &   l_va_total_FD ~= .
					
				
***************************************************************************************************************************************************************
******************************************************************* 6. Sector-Level Regressions
***************************************************************************************************************************************************

*************** 6.1 Compute the sector-level and aggregate markup

*Sector-level markup based on winsorized inverse markup
	
		
		bysort year naf_single: egen inv_mu_TL_sFSQ_t1_pos_w_h = wtmean(inv_mu_sepcal_TL_sFSQ_t1_pos_w), weight( share5 )
		
		gen mu_TL_sFSQ_t1_pos_w_h = 1/inv_mu_TL_sFSQ_t1_pos_w_h
		gen  l_mu_TL_sFSQ_t1_pos_w_h = log(mu_TL_sFSQ_t1_pos_w_h)
		
	

		
*Aggregate-level markup	based on winsorized inverse markup
	

		
		
		bysort year : egen inv_mu_TL_sFSQ_t1_pos_w_haggr = wtmean(inv_mu_sepcal_TL_sFSQ_t1_pos_w), weight( revShare )
		
		gen mu_TL_sFSQ_t1_pos_w_haggr = 1/inv_mu_TL_sFSQ_t1_pos_w_haggr
		gen  l_mu_TL_sFSQ_t1_pos_w_haggr = log(mu_TL_sFSQ_t1_pos_w_haggr)
		
		
		



*************** 6.2 Compute the sector-level HHI

*For full sample
	gen share5_sq = share5*share5
	bysort year naf_single: egen hhi = total(share5_sq), missing
	
	su share5_sq  hhi 


******************************************************************************************
*save the firm-level database
save BCG/data/full_firm_temp.dta, replace
******************************************************************************************

*************** 6.3 Construct Sector-Level Panel and merge with Sector-level data	


*Keep one observation by sector*year
	duplicates drop naf_single_num  year, force 

*save/USE the data
	save BCG/data/full_sec$specsDeta_${winlevel_gl}.dta, replace
	
	use BCG/data/full_sec$specsDeta_${winlevel_gl}.dta, clear
	
*set up the panel
	xtset naf_single_num year
	


*************** 6.4 Markup - HHI regressions
	
		***First-Difference
		
			******set up panel
			xtset naf_single_num year
						
			******Compute First-Difference
				gen FDinv_mu_TL_sFSQ_t1_pos_w_h = inv_mu_TL_sFSQ_t1_pos_w_h - L.inv_mu_TL_sFSQ_t1_pos_w_h
				gen FDhhi = hhi - L.hhi
				
				local top_tr $toptr_gl
				local bot_tr $bottr_gl
				local winlevel $winlevel_gl
								
				winsor  FDinv_mu_TL_sFSQ_t1_pos_w_h , p(.`winlevel') gen(FDinv_mu_TL_sFSQ_t1_pos_w_h_w)
				
				**Some summarry stats
				su FDinv_mu_TL_sFSQ_t1_pos_w_h FDinv_mu_TL_sFSQ_t1_pos_w_h_w FDhhi   
				
			*******Regressions
			
			
				reghdfe FDinv_mu_TL_sFSQ_t1_pos_w_h  FDhhi  if year>1994   , noabsorb  cl(year naf_single_num)
				outreg2 using BCG\results_sl\MarkupHhi_FD_$winlevel_gl$specsDeta , replace tex   keep(FDhhi) addtext(Sector FE, No, Year FE, No, Cluster , Sector  Year)

			

					



*************** 6.5 Markup Sector-Output 	
	


	
	reghdfe d.l_mu_TL_sFSQ_t1_pos_w_h  d.l_va_total   i.year  , absorb(naf_single_num)  cl(naf_single_num)
	outreg2 using BCG\results_sl\SectorMarkup_SectorOutput_$winlevel_gl$specsDeta , replace tex   keep(d.l_va_total) addtext(Sector FE, Yes, Year FE, Yes, Cluster , Sector  )
	

		
		

		
		
*************** 6.6 HHI- Sector-Output 	

	****taking logs
		gen l_hhi = log(hhi)
		gen FD_l_hhi = d.l_hhi
	


		reghdfe FD_l_hhi l_va_total_FD  i.year if year>1994   ,  absorb(naf_single_num)  cl( naf_single_num)
		outreg2 using BCG\results_sl\HHI_SectorOutput_$winlevel_gl$specsDeta , replace tex   keep(l_va_total_FD) addtext(Sector FE, Yes, Year FE, Yes, Sector-Year FE, No, Cluster ,  Sector)
		
		
			
		
		
	
*************** 6.7 Sector Markup - Aggregate-Output 


		***set up panel
		xtset naf_single_num year
	

		
		***Weight variable
		bysort naf_single_num: egen weightvar_va_total = mean(va_total)
		
	


		
		***Specification with First Difference
			xtset naf_single_num year
			
			****Compute aggregate output growth rate (log FD )
			capture gen FD_l_va_agg = l_va_agg - L.l_va_agg
			
			gen FD_l_mu_TL_sFSQ_t1_pos_w_h = l_mu_TL_sFSQ_t1_pos_w_h - L.l_mu_TL_sFSQ_t1_pos_w_h
		
			
			
			***Specificationin in First-Difference 
			reghdfe FD_l_mu_TL_sFSQ_t1_pos_w_h  FD_l_va_agg  [aweight=weightvar_va_total] if year>1994 , absorb(naf_single_num)  cl(naf_single_num)	
			outreg2 using BCG\results_sl\SectorMarkup_AggregateOutput_$winlevel_gl$specsDeta , replace tex   keep(FD_l_va_agg ) addtext(Sector FE, Yes, Cluster , Sector)
			
			reghdfe FD_l_mu_TL_sFSQ_t1_pos_w_h  FD_l_va_agg [aweight=weightvar_va_total] if year>1994 , absorb(naf_single_num)  cl(year)	
			outreg2 using BCG\results_sl\SectorMarkup_AggregateOutput_$winlevel_gl$specsDeta , append tex   keep(FD_l_va_agg ) addtext(Sector FE, Yes, Cluster , Year)
			
			
			
		
	
***************************************************************************************************************************************************
******************************************************************* 8. Within-Between
***************************************************************************************************************************************************


	*start from the firm-level data
	use BCG/data/full_firm_temp.dta, clear
	
	
*************** 8.1 Sector-Level Decomposition
	xtset firmsId year

	*Compute change in sector markup within term
		**average market share over two periods
		gen avg2per_share5 = 0.5*(share5 +l.share5)
		
		**Compute inverse markup change 
			**Compute inverse markup change 
			gen FDInvMuTLsFSQ_t1_pos_w=inv_mu_sepcal_TL_sFSQ_t1_pos_w-L.inv_mu_sepcal_TL_sFSQ_t1_pos_w
			
		
		**Compute within term 
		gen temp_within = avg2per_share5*FDInvMuTLsFSQ_t1_pos_w
		bysort naf_single year: egen invMuTLsFSQt1Pos_w_within = total(temp_within)
		
		
		**Move to sector-level panel and perform regression
		preserve
			***Keep one observation by sector, year
			duplicates drop naf_single year, force 
			
			***set up panel
			xtset naf_single_num year
			
			***Produce sector-level inverse markup change 
			gen inv_mu_TL_sFSQ_t1_pos_w_h_change = inv_mu_TL_sFSQ_t1_pos_w_h - L.inv_mu_TL_sFSQ_t1_pos_w_h
			
			***Produce reallocation term
			gen invMuTLsFSQt1Pos_w_realloc = inv_mu_TL_sFSQ_t1_pos_w_h_change - invMuTLsFSQt1Pos_w_within
			
			***Running the regression and saving coeffiecents
			statsby, by(naf_single) saving("BCG\results_sl\coeff_within_sector${specsDeta}_${winlevel_gl}.dta", replace): reg invMuTLsFSQt1Pos_w_within inv_mu_TL_sFSQ_t1_pos_w_h_change
			
			***Show results
			use "BCG\results_sl\coeff_within_sector${specsDeta}_${winlevel_gl}.dta", clear
			su, d
			
		 restore
	
	
	
* clean up 
clear all 
	
